﻿using Sustainalytics.AwsS3.Client;
using Sustainalytics.Entities;
using Sustainalytics.Utils.Configuration;


namespace Sustainalytics.Governance.Tasks
{
    public class S3FileHandler
    {
        private readonly string _filePath;

        private readonly S3Client _s3Client;
        public string S3BucketName { get; private set; }

        public S3FileHandler(string fileName, string s3BucketName = null)
        {
            S3BucketName = s3BucketName ?? SettingsHelper.Read("s3bucket", "dev-globalaccess-europe");
            _filePath = fileName;
            _s3Client =
                new S3Client(
                    new S3ClientSettings
                    {
                        AccessKey = SettingsHelper.Read("accesKeyId", "AKIAJEMY5FHDRQOHGPVA"),
                        SecretAccessKey =
                                SettingsHelper.Read(
                                    "awsSecretKey",
                                    "0MQCI2bE2E/pVDxR8aH2zWI4t1cagmokbSeMcMPy"),
                        ServiceEndPoint =
                                SettingsHelper.Read(
                                    "s3ServiceEndPoint",
                                    "https://s3-eu-west-1.amazonaws.com/")
                    });
        }

        public bool FileExists()
        {
            return _s3Client.FileExists(this.S3BucketName, this._filePath);
        }

        public PdfReportExecutionResult ReadFile()
        {
            return new PdfReportExecutionResult()
            {
                FileContent = null,
                S3Key = _filePath,
                S3Bucket = S3BucketName,
                UploadedToS3 = true
            };
        }

        public PdfReportExecutionResult WriteFile(byte[] content)
        {
            _s3Client.UploadFile(S3BucketName, _filePath, content);

            return new PdfReportExecutionResult()
            {
                FileContent = null,
                S3Key = _filePath,
                S3Bucket = S3BucketName,
                UploadedToS3 = true
            };
        }
    }
}
